import request from '@/utils/request'

// 每日一题相关接口类型定义
export interface DailyQuestionResponse {
  id: number
  date: string
  question: {
    id: number
    slug: string
    title: string
    content: string
    description?: string
    input_format?: string
    output_format?: string
    constraints?: string
    sample_input?: string
    sample_output?: string
    difficulty: 'Easy' | 'Medium' | 'Hard'
    time_limit: number
    memory_limit: number
    source?: string
    tags: Array<{
      id: number
      name: string
    }>
    solved_count: number
    view_count: number
    like_count: number
    submission_count: number
    accepted_count: number
    acceptance_rate: number
    author: {
      id: number
      username: string
      avatar_url: string
    }
    status: string
    audit_status: string
    created_at: string
    updated_at: string
  }
  is_solved: boolean
  attempt_count: number
  solved_at?: string
}

export interface UserDailyQuestionHistory {
  id: number
  date: string
  question: {
    id: number
    slug: string
    title: string
    difficulty: 'Easy' | 'Medium' | 'Hard'
    tags: Array<{
      id: number
      name: string
    }>
  }
  is_solved: boolean
  solved_at?: string
  attempt_count: number
  submission_id?: number
  first_attempt_at?: string
  last_attempt_at?: string
}

export interface UpdateDailyQuestionRequest {
  is_solved: boolean
  submission_id?: number
}

export interface DailyQuestionStats {
  total_daily_questions: number
  total_users: number
  solved_count: number
  today_solved_count: number
}

export interface PaginationInfo {
  page: number
  page_size: number
  total: number
  total_page: number
}

// API接口函数

/**
 * 获取今日每日一题
 */
export const getTodayDailyQuestion = (): Promise<DailyQuestionResponse | null> => {
  return request.get('/v1/daily-question/today').then(response => {
    const data = response.data
    // 检查success字段，如果为false且message为"暂无每日一题"，返回null
    if (data.success === false && data.message === "暂无每日一题") {
      return null
    }
    // 如果成功，返回数据
    if (data.success === true && data.data) {
      return data.data
    }
    // 其他情况抛出错误
    throw new Error(data.message || '获取每日一题失败')
  }).catch((error: any) => {
    // 未登录时接口返回401，视为暂无每日一题，避免页面出现“Network Error”
    if (error?.response?.status === 401) {
      return null
    }
    // 其他错误仍抛出，由组件展示错误或重试
    throw error
  })
}

/**
 * 获取用户每日一题历史记录
 */
export const getUserDailyQuestionHistory = (
  page: number = 1,
  pageSize: number = 10
): Promise<{
  history: UserDailyQuestionHistory[]
  pagination: PaginationInfo
}> => {
  return request.get('/v1/daily-question/history', {
    params: {
      page,
      page_size: pageSize
    }
  })
}

/**
 * 更新用户每日一题记录
 */
export const updateUserDailyQuestion = (data: UpdateDailyQuestionRequest): Promise<void> => {
  return request.post('/v1/daily-question/update', data)
}

/**
 * 获取每日一题统计信息
 */
export const getDailyQuestionStats = (): Promise<DailyQuestionStats> => {
  return request.get('/v1/daily-question/stats')
}

/**
 * 根据日期获取每日一题
 */
export const getDailyQuestionByDate = (date: string): Promise<DailyQuestionResponse> => {
  return request.get('/v1/daily-question/date', {
    params: {
      date
    }
  })
}
